﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SurveySystem.Core;
using System.Diagnostics;

namespace SurveySystem.Web
{
    public partial class MyGroups : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
        }

        protected void btnExecCreateGroup_Click(object sender, EventArgs e)
        {
            if (GroupsBLL.CreateGroup(tbGroupName.Text, User.Identity.Name))
            {
                multiView.ActiveViewIndex = 0;
                gridView_GroupsOfUsers.DataBind();
                lblGroupExists.Visible = false;
            }
            else
            {
                lblGroupExists.Visible = true;
            }
        }

        protected void btnCreateNewGroup_Click(object sender, EventArgs e)
        {
            multiView.ActiveViewIndex = 1;
        }
        
        protected void btnCancelCreateGroup_Click(object sender, EventArgs e)
        {
            multiView.ActiveViewIndex = 0;
        }

        protected void groupsOfUserDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["userName"] = User.Identity.Name;
        }

        protected void groupsOfUserDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
        }

        private LinkButton GetButton(TableCell cell)
        {
            foreach (Control c in cell.Controls)
                if (c is LinkButton) return c as LinkButton;
            return null;
        }

        private Label GetLabel(TableCell cell)
        {
            foreach (Control c in cell.Controls)
                if (c as Label != null) return c as Label;
            return null;
        }
    
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GroupOfUser gou = e.Row.DataItem as GroupOfUser;
            if (gou == null)
                return;
            TableCell cell = e.Row.Cells[1];
            Label labello = GetLabel(cell);
            switch (gou.UserRole)
            {
                case GroupMemberRole.NotMember:
                    LinkButton btn = GetButton(cell);
                    btn.Visible = true;
                    btn.CommandArgument = gou.GroupId.ToString();
                    break;
                default:
                    labello.Text = WebUtils.GetRoleString(gou.UserRole);
                    break;
            }
        }


        protected void joinableGroupsDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["userName"] = User.Identity.Name;
        }

        protected void gridView_JoinableGroups_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Join")
            {
                int groupId = Convert.ToInt32(e.CommandArgument);
                GroupsBLL.JoinRequest(User.Identity.Name, groupId);
                gridView_GroupsOfUsers.DataBind();
            }
        }

        protected void view1_Activate(object sender, EventArgs e)
        {
            lblGroupExists.Visible = false;
        }


        protected void chkOnlyMine_CheckedChanged(object sender, EventArgs e)
        {
            gridView_GroupsOfUsers.DataBind();
        }

        protected void btnFilterAll_Click(object sender, EventArgs e)
        {
            gridView_GroupsOfUsers.DataBind();
        }



 
    }
}
